﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AccCoreLib;
using Giraffe.Core.Common.Messengers;

namespace Giraffe.Core.ICQ
{
    class IcqConversation: IMessengerConversation
    {
        private readonly IMessengerContact _companion;
        private readonly AccSession _userSession;
        private IAccImSession _conversationSession;
        private List<IMessage> _history;

        public IcqConversation(IMessengerContact companion, AccSession userSession)
        {
            _conversationSession = userSession.CreateImSession(companion.ID, AccImSessionType.AccImSessionType_Im);
            _companion = companion;
            _userSession = userSession;
            _history = new List<IMessage>();
        }

        #region Implementation of IMessengerConversation

        public IMessengerContact Companion
        {
            get
            {
                return _companion;
            }
        }

        public DateTime SendMessage(string message)
        {
            IAccIm im = _userSession.CreateIm(message, null);
            _conversationSession.SendIm(im);
            return DateTime.Now;
        }

        public event Action<IMessage> MessageReceived;
        public event Action<IcqConversation> Closed;

        public IEnumerable<IMessage> GetHistory(TimeSpan period)
        {
            throw new NotImplementedException();
        }

        public void Close()
        {
           if (Closed != null)
           {
               Closed(this);
           }
        }

        public void ProcessNewMessage(IMessage message)
        {
            RiseMessageReceived(message);
        }

        #endregion

        private void RiseMessageReceived(IMessage message)
        {
            var handler = MessageReceived;
            if (handler != null)
            {
                handler.Invoke(message);
            }
        }
    }
}
